package mysql

import (
	"gorm.io/gorm"
	"manuscript-service/servicerpc/servicerpc"
)

type Service struct {
	gorm.Model
	Title   string `gorm:"type:varchar(50);not null;commit:'稿件标题'"`
	Like    int    `gorm:"type:int;not null;commit:'稿件点赞'"`
	Commit  int    `gorm:"type:int;not null;commit:'稿件评论'"`
	ClassId int    `gorm:"type:int;not null;commit:'种类编号'"`
}

func (s *Service) ServiceAdd(db *gorm.DB) error {
	return db.Create(s).Error
}

func (s *Service) FindServiceByTitle(db *gorm.DB, title string) error {
	return db.Where("title=?", title).First(s).Error
}
func FindService(db *gorm.DB, service []*servicerpc.ServiceList) []*servicerpc.ServiceList {
	db.Model(&Service{}).
		Select("services.id,title,`like`,`commit`,class_name").
		Joins("JOIN classes ON classes.id=services.class_id").Order("id ").
		Find(&service)

	return service
}
